package com.ruoyi.web.listener.custom;

import com.ruoyi.flow.entity.Constant;
import com.ruoyi.flow.entity.FireContext;
import com.ruoyi.flow.listener.FlowListener;
import com.ruoyi.web.domain.SysMod;
import com.ruoyi.web.listener.DefaultFlowListener;
import com.ruoyi.web.service.SysModFactory;

/**
 * 请假流程自己特殊的监听，实现自己的业务逻辑
 */
public class QjFlowListener extends DefaultFlowListener implements FlowListener {

    public QjFlowListener() {
    }

    @Override
    public void prepare(FireContext fireContext) {

    }

    @Override
    public void before(FireContext fireContext) {

        if (fireContext.getEvent().getId().equals(Constant.Event.START)) {
            SysModFactory.getSysModDataService(fireContext.getModId()).add(fireContext.getModId(), fireContext.getData());

            SysMod sysMod = sysModService.getById(fireContext.getModId());
            String sql = "update " + sysMod.getTableName() + " set instance_id=?,node_id=?,state=? where " + sysMod.getPrimaryKey() + "=?";
            dbAction.update(sql, new Object[]{
                    fireContext.getInstance().getInstanceId(),
                    fireContext.getTarget().getId(),
                    fireContext.getTarget().getState(),
                    fireContext.getData().getString(sysMod.getPrimaryKey())
            });
        }

    }

    @Override
    public void after(FireContext fireContext) {
        if ("delete".equals(fireContext.getEvent().getId())) {
            SysModFactory.getSysModDataService(fireContext.getModId()).delete(fireContext.getModId(), fireContext.getData());
            return;
        }

        SysModFactory.getSysModDataService(fireContext.getModId()).edit(fireContext.getModId(), fireContext.getData());

        SysMod sysMod = sysModService.getById(fireContext.getModId());
        String sql = "update " + sysMod.getTableName() + " set instance_id=?,node_id=?,state=? where " + sysMod.getPrimaryKey() + "=?";
        dbAction.update(sql, new Object[]{
                fireContext.getInstance().getInstanceId(),
                fireContext.getTarget().getId(),
                fireContext.getTarget().getState(),
                fireContext.getData().getString(sysMod.getPrimaryKey())
        });

    }
}
